//
//  ENsureTradView.m
//  AppNew
//
//  Created by 陈名邦 on 2019/4/6.
//  Copyright © 2019 华伟. All rights reserved.
//

#import "ENsureTradView.h"

@implementation ENsureTradView

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        [self initViews];
    }
    return self;
    
}

- (instancetype)init{
    
    self = [super init];
    if (self) {
        [self initViews];
    }
    return self;
    
}

- (void)initViews{
    
    _topButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 400.f)];
    _topButton.backgroundColor = [UIColor colorWithWhite:0.3 alpha:0.3];
    [_topButton addTarget:self
                   action:@selector(topAction:)
         forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_topButton];
    
    _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, _topButton.t_bottom, ScreenWidth, 400.f)];
    _bottomView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_bottomView];
    
    _limitPriceLabel = [[UILabel alloc] initWithFrame:CGRectMake(12.f, 28.f, 76.f, 24.f)];
    _limitPriceLabel.text = @"限额";
    _limitPriceLabel.font = [UIFont systemFontOfSize:14];
    _limitPriceLabel.textColor = [UIColor blackColor];
    _limitPriceLabel.textAlignment = NSTextAlignmentLeft;
    [_bottomView addSubview:_limitPriceLabel];
    
    _limitPriceVlaueTF = [[UITextField alloc] initWithFrame:CGRectMake(96.f, 28.f, self.t_width - 108, 24.f)];
    _limitPriceVlaueTF.text = @"";
    _limitPriceVlaueTF.userInteractionEnabled = NO;
    _limitPriceVlaueTF.font = [UIFont systemFontOfSize:18];
    _limitPriceVlaueTF.textColor = [UIColor colorFromHexRGB:@"4EAB92"];
    _limitPriceVlaueTF.textAlignment = NSTextAlignmentLeft;
    [_bottomView addSubview:_limitPriceVlaueTF];
    
    _rightLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 36.f, 24.f)];
    _rightLabel.text = @"";
    _rightLabel.font = [UIFont systemFontOfSize:16];
    _limitPriceVlaueTF.rightView = _rightLabel;
    _limitPriceVlaueTF.rightViewMode = UITextFieldViewModeUnlessEditing;

    
    _singlePriceLabel = [[UILabel alloc] initWithFrame:CGRectMake(12.f,  _limitPriceLabel.t_bottom + 24.f, 76.f, 24.f)];
    _singlePriceLabel.text = @"购买单价";
    _singlePriceLabel.font = [UIFont systemFontOfSize:14];
    _singlePriceLabel.textColor = [UIColor blackColor];
    _singlePriceLabel.textAlignment = NSTextAlignmentLeft;
    [_bottomView addSubview:_singlePriceLabel];
    
    _singlePriceVlaueLabel = [[UILabel alloc] initWithFrame:CGRectMake(96.f, _limitPriceLabel.t_bottom + 20.f, self.t_width - 108, 24.f)];
    _singlePriceVlaueLabel.text = @"6.72";
    _singlePriceVlaueLabel.font = [UIFont systemFontOfSize:18];
    _singlePriceVlaueLabel.textColor = [UIColor colorFromHexRGB:@"4EAB92"];
    _singlePriceVlaueLabel.textAlignment = NSTextAlignmentLeft;
    [_bottomView addSubview:_singlePriceVlaueLabel];
    _accountLabel = [[UILabel alloc] initWithFrame:CGRectMake(12.f, _singlePriceLabel.t_bottom + 24.f, 76.f, 24.f)];
    _accountLabel.text = @"购买数量";
    _accountLabel.font = [UIFont systemFontOfSize:14];
    _accountLabel.textColor = [UIColor blackColor];
    _accountLabel.textAlignment = NSTextAlignmentLeft;
    [_bottomView addSubview:_accountLabel];
    
    _accountValueTF = [[UITextField alloc] initWithFrame:CGRectMake(96.f, _singlePriceLabel.t_bottom + 20.f, self.t_width - 108, 32.f)];
    _accountValueTF.placeholder = @"请输入购买币种的数量";
    _accountValueTF.keyboardType =  UIKeyboardTypeNumberPad;
    _accountValueTF.delegate = self;
    _accountValueTF.font = [UIFont systemFontOfSize:14];
    _accountValueTF.textColor = [UIColor blackColor];
    _accountValueTF.textAlignment = NSTextAlignmentLeft;
    _accountValueTF.layer.borderWidth = 0.5;
    _accountValueTF.layer.borderColor = [UIColor colorFromHexRGB:@"DBDCDD"].CGColor;
    [_bottomView addSubview:_accountValueTF];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 44.f, 24.f)];
    label.text = @"USDT";
    label.font = [UIFont systemFontOfSize:16];
    _accountValueTF.rightView = label;
    _accountValueTF.rightViewMode = UITextFieldViewModeUnlessEditing;
    
    _totalPriceLabel = [[UILabel alloc] initWithFrame:CGRectMake(12.f, _accountLabel.t_bottom + 24.f, 76.f, 24.f)];
    _totalPriceLabel.text = @"总      价";
    _totalPriceLabel.font = [UIFont systemFontOfSize:14];
    _totalPriceLabel.textColor = [UIColor blackColor];
    _totalPriceLabel.textAlignment = NSTextAlignmentLeft;
    [_bottomView addSubview:_totalPriceLabel];
    
    _totalPriceValueTF = [[UITextField alloc] initWithFrame:CGRectMake(96.f, _accountLabel.t_bottom + 20.f, self.t_width - 108, 32.f)];
    _totalPriceValueTF.userInteractionEnabled = NO;
    _totalPriceValueTF.placeholder = @"请输入购买币种的总价";
    _totalPriceValueTF.font = [UIFont systemFontOfSize:14];
    _totalPriceValueTF.textColor = [UIColor blackColor];
    _totalPriceValueTF.textAlignment = NSTextAlignmentLeft;
    _totalPriceValueTF.layer.borderWidth = 0.5;
    _totalPriceValueTF.layer.borderColor = [UIColor colorFromHexRGB:@"DBDCDD"].CGColor;
    [_bottomView addSubview:_totalPriceValueTF];
    
    _ensureButton = [[UIButton alloc] initWithFrame:CGRectMake(16.f, _totalPriceValueTF.t_bottom + 24.f, ScreenWidth - 32.f, 32.f)];
    [_ensureButton setTitle:@"确认购买" forState:UIControlStateNormal];
    [_ensureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _ensureButton.backgroundColor = [UIColor colorFromHexRGB:@"3961F6"];
    [_ensureButton addTarget:self action:@selector(ensureAction:)
            forControlEvents:UIControlEventTouchUpInside];
    [_bottomView addSubview:_ensureButton];
    
    
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    
    [textField resignFirstResponder];
    _totalPriceValueTF.text = [NSString stringWithFormat:@"%.2f",[[_dataDic objectForKey:@"price"] floatValue] * [textField.text integerValue]];
    return YES;
    
}

- (void)ensureAction:(UIButton *)button{
    
    if ([_isSell isEqualToString:@"1"]) {
        
        NSString *url = [NSString stringWithFormat:@"http://119.160.234.147:8080/trade/okBuy?oid=%@&coin=%@",[_dataDic objectForKey:@"id"],_accountValueTF.text];
        [NetRequestTool get:url
                     params:nil
                    success:^(id  _Nonnull json) {
                        
                        BOOL flag = [[json objectForKey:@"flag"] boolValue];
                        if (flag) {
                            
                            [SVProgressHUD showSuccessWithStatus:@"购买成功"];
                            [self removeFromSuperview];
                            
                        }else{
                            
                            [SVProgressHUD showErrorWithStatus:[json objectForKey:@"msg"]];
                            [self removeFromSuperview];
                        }
                        
                    } failure:^(NSError * _Nonnull error) {
                        
                        [SVProgressHUD showErrorWithStatus:@"购买失败"];
                        [self removeFromSuperview];
                        
                    }];
        
    }else if ([_isSell isEqualToString:@"2"]){
        
        NSString *url = [NSString stringWithFormat:@"http://119.160.234.147:8080/trade/okSell?oid=%@&coin=%@",[_dataDic objectForKey:@"id"],_accountValueTF.text];
        [NetRequestTool get:url
                     params:nil
                    success:^(id  _Nonnull json) {
                        
                        BOOL flag = [[json objectForKey:@"flag"] boolValue];
                        if (flag) {
                            
                            [SVProgressHUD showSuccessWithStatus:@"出售成功"];
                            [self removeFromSuperview];
                            
                        }else{
                            
                            [SVProgressHUD showErrorWithStatus:[json objectForKey:@"msg"]];
                            [self removeFromSuperview];
                        }
                        
                    } failure:^(NSError * _Nonnull error) {
                        
                        [SVProgressHUD showErrorWithStatus:@"出售失败"];
                        [self removeFromSuperview];
                    }];
        
    }

//
    
}

- (void)topAction:(UIButton *)button{
    
    [self removeFromSuperview];
    
}

- (void)setDataDic:(NSDictionary *)dataDic{
    
    if (_dataDic != dataDic) {
        
        _dataDic = dataDic;
        _rightLabel.text = [NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"currency"]];
        _singlePriceVlaueLabel.text = [NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"price"]];
        NSInteger amount = [[_dataDic objectForKey:@"amount"] integerValue];
        CGFloat singlePrice = [[_dataDic objectForKey:@"price"] floatValue];
        _limitPriceVlaueTF.text = [NSString stringWithFormat:@"%@/%.2f",[_dataDic objectForKey:@"quota"],singlePrice *amount];
        
    }
    
}


@end
